import apiClient from '@/api/axios.js';

export class DisasterApi {
    // 发布灾害信息
    static async publishDisaster(params: {
        disasterType: string;
        disasterInfo: string;
        longitude: number;
        latitude: number;
        userID: number;
        status: 'unverified' | 'verified' | 'resolved';
        imgUrls?: string;
    }) {
        try {
            const response = await apiClient.post('/disaster/publish', params, {
                headers: {
                    'Content-Type': 'application/json',
                }
            });
            return response.data;
        } catch (error) {
            throw new Error(error.message || '发布灾害信息失败');
        }
    }

    // 获取灾害列表
    static async getDisasterList(params: {
        pageNum?: number;
        pageSize?: number;
        disasterType?: string;
        status?: 'unverified' | 'verified' | 'resolved';
    }) {
        try {
            const response = await apiClient.get('/disaster/list', {
                params,
                headers: {
                }
            });
            return response.data;
        } catch (error) {
            throw new Error(error.message || '获取灾害列表失败');
        }
    }

    // 获取灾害详情
    static async getDisasterDetail(disasterID: number) {
        try {
            const response = await apiClient.get(`/disaster/detail`, {
                params: { disasterID },
                headers: {
                }
            });
            return response.data;
        } catch (error) {
            throw new Error(error.message || '获取灾害详情失败');
        }
    }

    // 更新灾害状态
    static async updateDisasterStatus(disasterID: number, status: 'unverified' | 'verified' | 'resolved') {
        try {
            const response = await apiClient.put('/disaster/status', {
                disasterID,
                status
            }, {
                headers: {
                    'Content-Type': 'application/json',
                }
            });
            return response.data;
        } catch (error) {
            throw new Error(error.message || '更新灾害状态失败');
        }
    }

    // 上传灾害图片
    static async uploadDisasterImage(file: File) {
        try {
            const formData = new FormData();
            formData.append('file', file);
            
            const response = await apiClient.post('/disaster/upload', formData, {
                headers: {
                    'Content-Type': 'multipart/form-data',
                }
            });
            return response.data;
        } catch (error) {
            throw new Error(error.message || '上传灾害图片失败');
        }
    }
} 